<?php include('includes/start_session.php'); include('includes/login_required.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>e-class receive announcements</title>

	<link rel="stylesheet" href="styles/site.css" type="text/css" />
	<link rel="stylesheet" href="styles/less_padding.css" type="text/css" />
	
	<script src="scripts/scripts.js" type="text/javascript"></script>
</head>
<body onload="same_height()">
	<?php include('includes/header.php') ?>
	<div id="content" class="content_padding">
		<div id="courses">
			<?php
				include('db-api/txt-db-api.php');
				include('includes/course_info.php');
							
				function ShowProfessorCourses(&$showButton)
				{
					if($_SESSION['position'] == 'Stud')
						return;
					$db = new Database('Eclass');
					$rs = $db->executeQuery(
						'SELECT ReceiveMail, Courses.*
						 FROM Courses, ProfessorsCourses
						 WHERE ProfessorName = \'' . $_SESSION['username'] .
						 '\' AND CourseId = Id'
					);
					
					echo '<h4>Μαθήματα που είμαι καθηγητής</h4>';
					if(!$rs->next())
					{
						echo 'Δε βρέθηκαν μαθήματα που να είστε καθηγητής';
						return;
					}
					echo '<ul class="course_list">';
					$flag = true;
					do
					{
						list($recmail, $id, $name, $description, $subcat) = $rs->getCurrentValues();
						if($flag == true)
						{
							echo '<li class="first_course">';
							$flag = false;
						}
						else
							echo '<li>';

						if($recmail == 'false')
						{
							echo '<table><tr><td valign="middle">';
							echo '<input type="checkbox" name="courses[]" value="' . $id . '"/>';
							echo '</td><td>';
							$showButton = true;
						}
						echo '<div class="course">';
						
						echo '<a href="course.php?id=' . $id . '" class="title">' . $name . '</a>';
						ShowProfessors($id);
		
						if($recmail == 'true')
							echo '<a href="receive_announcements.php?action=remove&id=' . $id .
							 '" class="anchor">διακοπή λήψης ανακοινώσεων</a>';
						echo '</div>';
			
						if($recmail == 'false')
							echo '</td></tr></table>';
			
						echo '</li>';
					} while($rs->next());
					echo '</ul>';
				}
				
				function ShowStudentCourses(&$showButton)
				{
					$db = new Database('Eclass');
					$rs = $db->executeQuery(
						'SELECT Courses.*, ReceiveMail
						 FROM Courses, UsersCourses
						 WHERE UserName = \'' . $_SESSION['username'] .
						 '\' AND CourseId = Id'
					);
					
					echo '<h4>Μαθήματα που έχω δηλώσει</h4>';
					if(!$rs->next())
					{
						echo 'Δε βρέθηκαν μαθήματα που να έχετε δηλώσει';
						echo '<div class="right_align">';
						if($showButton == true)
						{
							echo '<input type="submit" value="Κατάθεση" /><br /><br />';
						}
						echo '<a href="#" class="course_anchor">Επιστροφή στην κορυφή</a></div>';
						return;
					}
					echo '<ul class="course_list">';
					$flag = true;
					do
					{
						list($id, $name, $description, $subcat, $recmail) = $rs->getCurrentValues();
						if($flag == true)
						{
							echo '<li class="first_course">';
							$flag = false;
						}
						else
							echo '<li>';

						if($recmail == 'false')
						{
							echo '<table><tr><td valign="middle">';
							echo '<input type="checkbox" name="courses[]" value="' . $id . '"/>';
							echo '</td><td>';
							$showButton = true;
						}

						echo '<div class="course">';
						
						echo '<a href="course.php?id=' . $id . '" class="title">' . $name . '</a>';
						ShowProfessors($id);

						if($recmail == 'true')
							echo '<a href="receive_announcements.php?action=remove&id=' . $id .
							 '" class="anchor">διακοπή λήψης ανακοινώσεων</a>';
						echo '</div>';
						
						if($recmail == 'false')
							echo '</td></tr></table>';
						
						echo '</li>';
					} while($rs->next());
					echo '</ul>';
					echo '<div class="right_align">';
					if($showButton == true)
					{
						echo '<input type="submit" value="Κατάθεση" /><br /><br />';
					}
					echo '<a href="#" class="course_anchor">Επιστροφή στην κορυφή</a>';
					echo '</div>';
				}
				
				$_SESSION['previous_page'] = 'user_announcements.php';
            	if(isset($_GET['message']) && $_GET['message'] == '1')
            		echo '<div class="center_align"><span class="error">Δεν επιλέξατε κανένα μάθημα</span></div><br />';
				if(isset($_GET['message']) && $_GET['message'] == '2')
            		echo '<div class="center_align"><span class="success">Η αλλαγή έγινε με επιτυχία</span></div><br />';
            	if(isset($_GET['message']) && $_GET['message'] == '3')
            		echo '<div class="center_align"><span class="error">Το μάθημα που επιλέξατε δεν υπάρχει</span></div><br />';
				echo '<i>(Μπορείτε να επιλέξετε τα μαθήματα στα οποία θέλετε να λαμβάνετε ανακοινώσεις με mail)</i>';
				echo '<form action="receive_announcements.php?action=add" method="post">';
				$showButton = false;
				ShowProfessorCourses($showButton);
				ShowStudentCourses($showButton);
				echo '</form>';
			?>
		</div>
	</div>
	<?php include('includes/footer.php') ?>
</body>
</html>